package common

import (
	"github.com/emicklei/go-restful/v3"
	"github.com/rs/zerolog/log"
)

type Response struct {
	Code    string      `json:"code,omitempty"`
	Message string      `json:"message,omitempty"`
	Data    interface{} `json:"data"`
}

// build 模式

func (r *Response) WithCode(code string) *Response {
	r.Code = code
	return r
}

func (r *Response) WithMessage(message string) *Response {
	r.Message = message
	return r
}

func (r *Response) WithData(data interface{}) *Response {
	r.Data = data
	return r
}

func OK(data interface{}) *Response {

	return &Response{
		Code:    "0",
		Message: "Success",
		Data:    data,
	}

}

func Fail(code string, message string) *Response {
	return &Response{
		Code:    code,
		Message: message,
		Data:    nil,
	}

}

// Out 返回给界面
func Out(response *restful.Response, commResult *Response) {
	err := response.WriteAsJson(commResult)
	if err != nil {
		log.Error().Msg("输出异常")
	}
}



